package com.ychug.framework.business.service;

import com.ychug.common.constant.UserConstants;
import com.ychug.common.utils.DateUtils;
import com.ychug.common.utils.StringUtils;
import com.ychug.framework.util.PasswordUtil;
import com.ychug.framework.web.exception.UserNotExistsException;
import com.ychug.framework.web.exception.UserPasswordNotMatchException;
import com.ychug.service.system.entity.SysUserEntity;
import com.ychug.service.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 登录校验方法
 * 
 * @author ruoyi
 */
@Component
public class SysLoginService
{

    @Autowired
    private ISysUserService userService;

    /**
     * 登录
     */
    public SysUserEntity login(String username, String password,String validateCode)
    {
        // 验证码校验
//        if (!StringUtils.isEmpty(ServletUtils.getRequest().getAttribute(ShiroConstants.CURRENT_CAPTCHA)))
//        {
//            AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error")));
//            throw new CaptchaException();
//        }
//        // 用户名或密码为空 错误
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password))
        {
            throw new UserNotExistsException();
        }
//        // 密码如果不在指定范围内 错误
        if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
                || password.length() > UserConstants.PASSWORD_MAX_LENGTH)
        {
          //  AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
            throw new UserPasswordNotMatchException();
        }
//
//        // 用户名不在指定范围内 错误
//        if (username.length() < UserConstants.USERNAME_MIN_LENGTH
//                || username.length() > UserConstants.USERNAME_MAX_LENGTH)
//        {
//            AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
//            throw new UserPasswordNotMatchException();
//        }

        // 查询用户信息
        SysUserEntity user = userService.selectUserByLoginName(username);
        if (user == null && maybeMobilePhoneNumber(username))
        {
            user = userService.selectUserByPhoneNumber(username);
        }

        if (user == null && maybeEmail(username))
        {
            user = userService.selectUserByEmail(username);
        }

        if (!matches(user, password))
        {
            throw new UserPasswordNotMatchException();
        }

        recordLoginInfo(user);
        return user;
    }

    public boolean matches(SysUserEntity user, String newPassword)
    {
        String pass = getNewPassword(newPassword);
        return user.getPassword().equals(pass);
    }


    public String  getNewPassword(String newPassword){
        String newPass = PasswordUtil.MD5Encode(newPassword, "UTF8", true);
        System.out.println("原密码："+newPassword + " 加密后的密码"+newPass);
        return newPass;
    }


    private boolean maybeEmail(String username)
    {
        if (!username.matches(UserConstants.EMAIL_PATTERN))
        {
            return false;
        }
        return true;
    }

    private boolean maybeMobilePhoneNumber(String username)
    {
        if (!username.matches(UserConstants.MOBILE_PHONE_NUMBER_PATTERN))
        {
            return false;
        }
        return true;
    }

    /**
     * 记录登录信息
     */
    public void recordLoginInfo(SysUserEntity user)
    {
        user.setLoginIp("127.0.0.1");
        user.setLoginDate(DateUtils.getNowDate());
        userService.updateUserInfo(user);
    }
}
